<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            虚拟入库
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 库位" id="workCellCode" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell()"></i>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描箱号" id="boxnum" value="" onclick="getIdSelector();"
                            onfocus="focusFn(this)">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">托码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描/输入 托号" id="trayyinfoid" value="" onclick="getIdSelector();"
                            onfocus="focusFn(this)">
                    </div>
                </div>
            </li>
           

        </ul>
        <p></p>
        <p></p>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="saveSelect()">保存</div>

    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var workCellGid = ""; //来源库位GID
    var workCellCode = ""; //来源库位编码
    var workCellName = ""; //来源库位名称

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //初始化
    apiready = function () {

        $("#workCellCode").focus();
        idSelector = "workCellCode";
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：库位
            if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询库位
                getWorkCell(ret, idSelector);

            } else if (idSelector == 'boxnum') {
                $("#boxnum").blur();

                //条码解析+自栋绑定
                tmSplit(ret);
            } else if (idSelector == 'trayyinfoid') {
                $("#trayyinfoid").blur();
                //发货单号赋值
                $("#trayyinfoid").val(ret);
                saveTray();
            }
        });
    }

    //7:库位扫描校验
    function getWorkCell(code, inputId, flag) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "in_rk", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: "", //仓库GID
                workCellCode: code, //库位编码
                relWorkCellGid: "", //目标库位
                appVersion: api.appVersion,
                tm: "",
                centerClass: "",
                workCellFlag: "", //原库位目标库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCellGid = list[0].workCellGid;
                    workCellCode = list[0].workCellCode;
                    workCellName = list[0].workCellName;
                    document.getElementById(inputId).value = list[0].workCellCode + ":" + list[0].workCellName;
                    $("#boxnum").val("");
                    $("#boxnum").select();
                    idSelector = "boxnum";

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCellCode").focus();
                    $("#workCellCode").select();
                }

            }
        });
    }

    //8：选择库位
    function goSelectWorkCell(workCellFlag) {
        var tm = $("#tm").val();
        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {

                flag: "in_rk",
                name: "uwmInOfStockVirtual",
                tm: tm,
                workCenterGid: "",
                centerClass: "",
                relWorkCellGid: "",
                workCellFlag: ""
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name) {
        workCellGid = id;
        workCellCode = code;
        workCellName = name;
        document.getElementById('workCellCode').value = code + ":" + name;

        api.setFocus({
            inputId: 'boxnum',
            focus: true,
        });
    }
    //7；PDA自带扫描回车事件-库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCellCode").val(sm);
            //校验改箱码是否存在
            getWorkCell(sm, "workCellCode");
        }
    });

    function saveSelect(){
        if (trayyinfoid != "" && trayyinfoid != null && trayyinfoid != undefined) {
            saveTray();
        } else if (boxnum != "" && boxnum != null && boxnum != undefined){
            save();
        }else{
            saveTray();
        }
    }

    //虚拟入库 扫托
    function saveTray() {

        //托码
        var trayyinfoid = $("#trayyinfoid").val();

        var boxnum = $("#boxnum").val();

        //托码
        if (workCellGid == "" || workCellGid == null || workCellGid == undefined) {
            api.toast({
                msg: '请扫描库位！',
                duration: 5000
            });
            return;
        }


        //托码
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            api.toast({
                msg: '请扫描托码！',
                duration: 5000
            });
            return;
        } else if (boxnum != "" && boxnum != null && boxnum != undefined) {
            api.toast({
                msg: '仅支持扫箱或扫托入库，请重新扫描！',
                duration: 5000
            });
            $("#trayyinfoid").val("");
            $("#boxnum").val("");
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmInOfStockVirtualTray";
        var data = {
            values: {

                trayyinfoid: trayyinfoid, //托号
                appVersion: api.appVersion,
                boxnum: boxnum,
                workCellGid: workCellGid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    //清空表单
                    $("#trayyinfoid").val("");
                    $("#boxnum").val("");
                    $("#trayyinfoid").select();
                    idSelector = "trayyinfoid";

                    //失败
                } else if (myobj.code == '1') {

                    // $("#trayyinfoid").val("");
                    // $("#trayyinfoid").select();
                    // idSelector = "trayyinfoid";

                    // api.hideProgress();

                    // var div = document.getElementById("sure")
                    // div.innerText = myobj.message;

                    $("#trayyinfoid").val("");
                    api.hideProgress();
                    $("#trayyinfoid").select();
                    idSelector = "trayyinfoid";
                   // clearTabltDiv(myobj.message);
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });


                }
            } else {

                $("#trayyinfoid").val("");
                $("#trayyinfoid").select();
                idSelector = "trayyinfoid";
                api.hideProgress();
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }

        });
    }

    //虚拟入库 扫箱
    function save() {


        var boxnum = $("#boxnum").val();

        var trayyinfoid = $("#trayyinfoid").val();

        //托码 
        if (workCellGid == "" || workCellGid == null || workCellGid == undefined) {
            api.toast({
                msg: '请扫描库位！',
                duration: 5000
            });
            return;
        }


        //托码
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '请扫描箱码！',
                duration: 5000
            });
            return;
        } else if (trayyinfoid != "" && trayyinfoid != null && trayyinfoid != undefined) {
            api.toast({
                msg: '仅支持扫箱或扫托入库，请重新扫描！',
                duration: 5000
            });
            $("#trayyinfoid").val("");
            $("#boxnum").val("");
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmInOfStockVirtual";
        var data = {
            values: {
                appVersion: api.appVersion,
                boxnum: boxnum,
                workCellGid: workCellGid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    //清空表单
                    $("#trayyinfoid").val("");
                    $("#boxnum").val("");
                    $("#boxnum").select();
                    idSelector = "boxnum";

                    //失败
                } else if (myobj.code == '1') {
                    $("#boxnum").val("");
                    api.hideProgress();
                    $("#boxnum").select();
                    idSelector = "boxnum";
                   // clearTabltDiv(myobj.message);
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    // $("#boxnum").val("");
                    // $("#boxnum").select();
                    // idSelector = "boxnum";

                    // api.hideProgress();

                    // var div = document.getElementById("sure")
                    // div.innerText = myobj.message;


                }
            } else {
                $("#boxnum").val("");
                $("#boxnum").select();
                idSelector = "boxnum";
                api.hideProgress();
                // alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
                // $("#boxnum").val("");
                // $("#boxnum").select();
                // idSelector = "boxnum";
                // api.hideProgress();
                // //alert(JSON.stringify(err));
                // alert("连接超时，请耐心等待");
            }

        });
    }
    //条码拆分
    function tmSplit(tm) {
        //条码解析
        var arr = tm.split(",");
        //物料号
        $("#mrlCode").val(arr[1]);
        //数量
        $("#qty").val(arr[2]);
        //箱号
        $("#boxnum").val(arr[9]);
        //条码
        $("#tm").val(tm);

        //发货单选中
        $("#boxnum").select();

        //自动验证
        save();
    }

    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#boxnum").blur();
                console.log(tm);

                //条码拆分+自动验证
                tmSplit(tm);
            }

        }
    });

    //托码监听回车事件
    $("#trayyinfoid").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var trayyinfoid = $(this).val();
        if (keycode == '13') {
            if (trayyinfoid != null && trayyinfoid != "" && trayyinfoid != undefined) {
                $("#trayyinfoid").blur();
                console.log(trayyinfoid);

                $("#trayyinfoid").val(trayyinfoid);
                saveTray();
            }

        }
    });




    function focusFn(ele) {
        $api.attr(ele, 'readonly', 'readonly');
        setTimeout(() => {
            $api.removeAttr(ele, 'readonly');
        }, 200);
    };

    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>